package commons.base.serialization.xml;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

public class XStreamExample {
	public static void main(String[] args)throws Exception{
		t2();
	}
	
	public static void t2(){
		XStream stream = new XStream();
		stream.processAnnotations(Model.class);
		System.err.println(stream.toXML(new Model()));
	}
	
	public static void t1()throws Exception{
		XStream stream = new XStream();
		stream.alias("user_buyer_get_response", UserBuyerGetResponse.class);
		UserBuyerGetResponse response = (UserBuyerGetResponse)stream.fromXML(readXml("user_buyer_get_response.xml"));
		System.err.println(response.getUser().getNick());
		
		stream.processAnnotations(ErrorResponse.class);
		ErrorResponse errorResponse = (ErrorResponse)stream.fromXML(readXml("error_response.xml"));
		System.err.println(errorResponse.toString());
		System.err.println(stream.toXML(errorResponse));
		
//		Object obj = stream.fromXML(readXml("error_response.xml"));
//		if(obj instanceof ErrorResponse){
//			ErrorResponse er = (ErrorResponse)obj;
//		}else{
//			UserBuyerGetResponse ubr = (UserBuyerGetResponse)obj;
//		}
			
	}
	
	private static String readXml(String name) throws Exception{
		Path path = Paths.get(XStreamExample.class.getResource("/" + name).toURI());
		
		return new String(Files.readAllBytes(path), "UTF-8");
	}
	
	public static class UserBuyerGetResponse {

	    private User       user;

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

	}
	
	public static class User {

	    private String nick;
	    private String sex;
	    private String avatar;
	    
		public String getNick() {
			return nick;
		}
		public void setNick(String nick) {
			this.nick = nick;
		}
		public String getSex() {
			return sex;
		}
		public void setSex(String sex) {
			this.sex = sex;
		}
		public String getAvatar() {
			return avatar;
		}
		public void setAvatar(String avatar) {
			this.avatar = avatar;
		}

	}
	
	@XStreamAlias("error_response")
	public static class ErrorResponse {
		private int code;
		private String msg;
		@XStreamAlias("sub_code")
		private String subCode;
		@XStreamAlias("sub_msg")
		private String subMsg;
		// 忽略字段
		@XStreamOmitField
		private String igoneField;
		
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public String getSubCode() {
			return subCode;
		}
		public void setSubCode(String subCode) {
			this.subCode = subCode;
		}
		public String getSubMsg() {
			return subMsg;
		}
		public void setSubMsg(String subMsg) {
			this.subMsg = subMsg;
		}
		
		@Override
		public String toString(){
			return code + "," + msg + "," + subCode + "," + subMsg;
		}
		public String getIgoneField() {
			return igoneField;
		}
		public void setIgoneField(String igoneField) {
			this.igoneField = igoneField;
		}
	}
	
	@XStreamAlias("model")
	public static class Model{
		private Date date = new Date();
		private List<User> list = new ArrayList<User>();
		private Map<String, User> map = new HashMap<String, User>();
		
		public Model(){
			list.add(new User());
			map.put("user", new User());
		}

		public Date getDate() {
			return date;
		}

		public void setDate(Date date) {
			this.date = date;
		}

		public List<User> getList() {
			return list;
		}

		public void setList(List<User> list) {
			this.list = list;
		}

		public Map<String, User> getMap() {
			return map;
		}

		public void setMap(Map<String, User> map) {
			this.map = map;
		}
		
	}

}
